package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.exception.BusinessException;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Sound;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import com.itheima.vo.SoundVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Component
public class SoundManager {


    @Autowired
    FastFileStorageClient client;

    @Autowired
    FdfsWebServer server;

    @DubboReference
    SoundService soundService;
    @DubboReference
    UserInfoService userInfoService;

    /**
     * 发送语音
     * @param soundFile
     * @throws IOException
     */
    public void sendSound(MultipartFile soundFile) throws IOException {
        //1.获取登录者id
        Long userId = UserHolder.get().getId();
        String date = DateUtil.formatDate(new Date());
        //2.查询该用户今天发送了多少条语音
        Integer num = soundService.findNum(date, userId);

        if (num >= 5){
            throw new BusinessException("您已超出今日发送语音上限");
        }
        //3.将文件上传到fds存储器上获得存储路径
        StorePath soundPath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), FileUtil.extName(soundFile.getOriginalFilename()), null);
        //4.将文件路径封装到Sound对象中;
        Sound sound = new Sound();
        sound.setUserId(userId);
        sound.setSoundUrl(server.getWebServerUrl()+soundPath.getFullPath());
        sound.setCreated(System.currentTimeMillis());
        sound.setDate(date);//设置语音发送日期
        //5.保存语音
        soundService.save(sound);
    }

    /**
     * 接收语音
     * @return
     */
    public ResponseEntity acceptSound() {
        //1.获取用户id
        Long userId = UserHolder.get().getId();
        String date = DateUtil.formatDate(new Date());
        //2.随机获取一条语音
        Sound sound = soundService.findOneSound(userId);

        if (sound == null){
            throw new BusinessException("暂无合适语音推荐,请稍后再试");
        }
        //3.查询剩余获取次数
        Integer remainingTimes = soundService.remainingTimes(date,userId);
        //3.1判断剩余次数
        if (remainingTimes < 0){
            //如果次数用完,者不能获取语音
            throw new BusinessException("您已超出今日接收语音上限");
        }
        //4.根据声音用户的id获取用户详情
        UserInfo userInfo = userInfoService.findById(sound.getUserId());

        //5.封装数据;
        SoundVo vo = new SoundVo();
        BeanUtil.copyProperties(userInfo,vo);
        vo.setSoundUrl(sound.getSoundUrl());
        vo.setRemainingTimes(remainingTimes);

        //6.返回数据
        return ResponseEntity.ok(vo);

    }
}
